﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
using System.IO;
using Microsoft.Win32;
using Microsoft.MediaPlayer.Interop;

namespace BMproductions.Media.MediaPlayer.WMPPlus {
	public abstract class WMPPlugEvents: WMPPlug, IWMPEvents {
		AxHost.ConnectionPointCookie cookie;

		[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
		private static extern int SearchPath(
			[MarshalAs(UnmanagedType.LPTStr)] string lpPath,
			[MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
			[MarshalAs(UnmanagedType.LPTStr)] string lpExtension,
			int nBufferLength,
			[Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpBuffer,
			out IntPtr lpFilePart);

		protected override void Init() {
			base.Init();
			this.cookie = new AxHost.ConnectionPointCookie(this.core, this, typeof(IWMPEvents));
		}

		public override void Dispose() {
			base.Dispose();
			if (this.cookie != null)
				this.cookie.Disconnect();
			this.cookie = null;
		}

		public static void RegisterUIPlugin(Guid clsid, string name, string label, string description, int capabilities) {
			var classes = Registry.ClassesRoot;
			using (var key0 = classes.OpenSubKey(String.Format(@"CLSID\{0:B}", clsid), true)) {
				if (key0 != null) {
					key0.SetValue(null, name);
					using (var key = key0.OpenSubKey("InprocServer32", true)) {
						if (key != null) {
							string lib = (string)key.GetValue(null);
							if (!Path.IsPathRooted(lib)) {
								var sb = new StringBuilder(260);
								IntPtr dummy;
								if (SearchPath(null, lib, null, 260, sb, out dummy) > 0) {
									key.SetValue(null, sb.ToString());
								}
							}
						}
					}
				}
			}
			var root = Registry.LocalMachine;
			using (var key = root.CreateSubKey(Path.Combine(WMPConstants.PLUGIN_INSTALLREGKEY, clsid.ToString("B")))) {
				WMPPlusSettings.SaveRegistryString(key, WMPConstants.PLUGIN_INSTALLREGKEY_FRIENDLYNAME, label);
				WMPPlusSettings.SaveRegistryString(key, WMPConstants.PLUGIN_INSTALLREGKEY_DESCRIPTION, description);
				WMPPlusSettings.SaveRegistryNumber(key, WMPConstants.PLUGIN_INSTALLREGKEY_CAPABILITIES, capabilities);
			}
		}

		public static void UnregisterUIPlugin(Guid clsid) {
			var root = Registry.LocalMachine;
			root.DeleteSubKey(Path.Combine(WMPConstants.PLUGIN_INSTALLREGKEY, clsid.ToString("B")), true);
		}

		#region IWMPEvents implementation
		public virtual void OpenStateChange(int NewState) { }
		public virtual void PlayStateChange(int NewState) { }
		public virtual void AudioLanguageChange(int LangID) { }
		public virtual void StatusChange() { }
		public virtual void ScriptCommand(string scType, string Param) { }
		public virtual void NewStream() { }
		public virtual void Disconnect(int Result) { }
		public virtual void Buffering(bool Start) { }
		public virtual void Error() { }
		public virtual void Warning(int WarningType, int Param, string Description) { }
		public virtual void EndOfStream(int Result) { }
		public virtual void PositionChange(double oldPosition, double newPosition) { }
		public virtual void MarkerHit(int MarkerNum) { }
		public virtual void DurationUnitChange(int NewDurationUnit) { }
		public virtual void CdromMediaChange(int CdromNum) { }
		public virtual void PlaylistChange(object Playlist, WMPPlaylistChangeEventType change) { }
		public virtual void CurrentPlaylistChange(WMPPlaylistChangeEventType change) { }
		public virtual void CurrentPlaylistItemAvailable(string bstrItemName) { }
		public virtual void MediaChange(object Item) { }
		public virtual void CurrentMediaItemAvailable(string bstrItemName) { }
		public virtual void CurrentItemChange(object pdispMedia) { }
		public virtual void MediaCollectionChange() { }
		public virtual void MediaCollectionAttributeStringAdded(string bstrAttribName, string bstrAttribVal) { }
		public virtual void MediaCollectionAttributeStringRemoved(string bstrAttribName, string bstrAttribVal) { }
		public virtual void MediaCollectionAttributeStringChanged(string bstrAttribName, string bstrOldAttribVal, string bstrNewAttribVal) { }
		public virtual void PlaylistCollectionChange() { }
		public virtual void PlaylistCollectionPlaylistAdded(string bstrPlaylistName) { }
		public virtual void PlaylistCollectionPlaylistRemoved(string bstrPlaylistName) { }
		public virtual void PlaylistCollectionPlaylistSetAsDeleted(string bstrPlaylistName, bool varfIsDeleted) { }
		public virtual void ModeChange(string ModeName, bool NewValue) { }
		public virtual void MediaError(object pMediaObject) { }
		public virtual void OpenPlaylistSwitch(object pItem) { }
		public virtual void DomainChange(string strDomain) { }
		public virtual void SwitchedToPlayerApplication() { }
		public virtual void SwitchedToControl() { }
		public virtual void PlayerDockedStateChange() { }
		public virtual void PlayerReconnect() { }
		public virtual void Click(short nButton, short nShiftState, int fX, int fY) { }
		public virtual void DoubleClick(short nButton, short nShiftState, int fX, int fY) { }
		public virtual void KeyDown(short nKeyCode, short nShiftState) { }
		public virtual void KeyPress(short nKeyAscii) { }
		public virtual void KeyUp(short nKeyCode, short nShiftState) { }
		public virtual void MouseDown(short nButton, short nShiftState, int fX, int fY) { }
		public virtual void MouseMove(short nButton, short nShiftState, int fX, int fY) { }
		public virtual void MouseUp(short nButton, short nShiftState, int fX, int fY) { }
		#endregion
	}
}
